package com.lovo.shopping.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 直连模式的MQ配置
 */
@Configuration
@EnableRabbit
public class DirectMQConfig {
    @Bean
    public Exchange directExchange(){
        // 创建直连的交换机
        return ExchangeBuilder.directExchange("shop.direct.exchange").durable(true).build();
    }
    @Bean
    public Queue directQueue(){
        HashMap args = new HashMap();
        // 为当前队列配置死信队列
        args.put("x-dead-letter-exchange","dead.letter.exchange");
        args.put("x-dead-letter-routing-key","dead.letter.rk");
        // 配置当前队列最大消息数
        args.put("x-max-length",10);
        // 当消息数超过当前队列的最大数采用的策略
        args.put("x-overflow","reject-publish-dlx");
        // 创建直连的队列
       return new Queue("shop.direct.queue",true,false,false,args);
    }
    @Bean
    public Queue registerQueue(){
        // 创建注册信息的队列
        return new Queue("register.direct.queue",true);
    }
    @Bean
    public Queue seckillQueue(){
        // 创建秒杀消息的队列
        return new Queue("seckill.direct.queue",true);
    }
    @Bean
    public Queue payQueue(){
        HashMap args = new HashMap();
        // 为当前队列配置死信队列
        args.put("x-dead-letter-exchange","dead.letter.exchange");
        args.put("x-dead-letter-routing-key","dead.letter.pay.rk");
        // 创建支付消息的队列
        return new Queue("pay.direct.queue",true,false,false,args);
    }
    @Bean
    public Binding directExchangeQueueBinding(){
        // 将交换机和队列进行绑定
        return BindingBuilder.bind(directQueue()).to(directExchange()).with("test_direct_rk").noargs();
    }
    @Bean
    public Binding directExchangeRegisterQueueBinding(){
        // 将交换机和注册队列绑定
        return BindingBuilder.bind(registerQueue()).to(directExchange()).with("register_direct_rk").noargs();
    }
    @Bean
    public Binding directExchangeSeckillQueueBinding(){
        // 将交换机和注册队列绑定
        return BindingBuilder.bind(seckillQueue()).to(directExchange()).with("seckill_direct_rk").noargs();
    }
    @Bean
    public Binding directExchangePayQueueBinding(){
        // 将交换机和注册队列绑定
        return BindingBuilder.bind(payQueue()).to(directExchange()).with("pay_direct_rk").noargs();
    }
}
